/*
 * @file    port.c
 * @author  Frederic Pillon <frederic.pillon@st.com> for STMicroelectronics.
 * @brief   Include source of FreeRTOS portable layer file to match Arduino library format
 */
#include <Arduino.h>

#ifndef __CORTEX_M
#pragma GCC error  "no \"__CORTEX_M\" definition"
#endif

#if (__CORTEX_M == 0x00U)
#include "../portable/GCC/ARM_CM0/port.c"
#endif

#if (__CORTEX_M == 0x03U)
/* MPU not supported */
#if 0 /*(__MPU_PRESENT == 1)*/
#include "../portable/GCC/ARM_CM3_MPU/port.c"
#else
#include "../portable/GCC/ARM_CM3/port.c"
#endif
#endif

#if (__CORTEX_M == 0x04U)
/* MPU not supported */
#if 0 /*(__MPU_PRESENT == 1)*/
#include "../portable/GCC/ARM_CM4_MPU/port.c"
#else
#include "../portable/GCC/ARM_CM4F/port.c"
#endif
#endif

#if (__CORTEX_M == 0x07U)
/* MPU not supported */
#if 0 /*(__MPU_PRESENT == 1)*/
#include "../portable/GCC/ARM_CM7_MPU/r0p1/port.c"
#else
#include "../portable/GCC/ARM_CM7/r0p1/port.c"
#endif
#endif
#if( configASSERT_DEFINED == 1 )
void vSetVarulMaxPRIGROUPValue( void )
{
    volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
    volatile uint8_t ucMaxPriorityValue;
    /* Determine the number of priority bits available.  First write to all
    possible bits. */
    *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
    /* Read the value back to see how many bits stuck. */
    ucMaxPriorityValue = *pucFirstUserPriorityRegister;
    /* Calculate the maximum acceptable priority group value for the number
    of bits read back. */
    ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
    while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
    {
        ulMaxPRIGROUPValue--;
        ucMaxPriorityValue <<= ( uint8_t ) 0x01;
    }
#ifdef __NVIC_PRIO_BITS
    {
        /* Check the CMSIS configuration that defines the number of
        priority bits matches the number of priority bits actually queried
        from the hardware. */
        configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
    }
#endif
#ifdef configPRIO_BITS
    {
        /* Check the FreeRTOS configuration that defines the number of
        priority bits matches the number of priority bits actually queried
        from the hardware. */
        configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
    }
#endif
    /* Shift the priority group value back to its position within the AIRCR
    register. */
    ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
    ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
}
#endif /* conifgASSERT_DEFINED */
